<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */

$intBT = (int)$_data["Bet_Type"];
$intGID = (int)$_data["GambleID"];
$strNumber = trim($_data["strNumber"]);
print $_data["Odds"]."D==";
$flOdds = (float)$_data["Odds"];
print $flOdds."A==";
$flGapOdds = (float)$_data["GapOdds"];
$isGapUpdate = (int)$_data["isGapUpdate"];
$isSpecial = (int)$_data["isSpecial"];

if($intBT < 1 || $intGID < 1 || $intLID < 1){
	$GLOBALS["errormessage"] = "Data Error!!";
	$GLOBALS["errorhidden"] = "\nBetType:".$intBT."\nGambleID:".$intGID."\nLottoID:".$intLID."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

$arrNum = explode(",", $strNumber);
//若數字是空陣列，就不處理
if(sizeof($arrNum) == 0){
	$GLOBALS["errormessage"] = "No Number Data!!";
	$GLOBALS["errorhidden"] = "\nNumber:".$strNumber."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
foreach($arrNum as $k => $v){
	$arrNum[$k] = (int)$v;
}

$arrBT = array();
$arrBT = array($intBT);

$arrOID = array();
PMA_mysql_query("BEGIN");
if($isGapUpdate == 1){
	$strSQL = "select Number, Odds from `bet_TodayGame".$intLID."` where Bet_Type = ".$intBT." and GambleID = ".$intGID." and Number in (".implode(",", $arrNum).")";
	if(!$RS = PMA_mysql_query($strSQL)){
		@PMA_mysql_query("ROLLBACK");
		ErrMsgOutputHTML("Error", "", 1);
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		$flOdds = $Rows -> Odds + $flGapOdds;
		if($flOdds < 0)$flOdds = 0;
		//找出 Apoint
		$intAN = (int)$_arrGLineNumToApointNum[$intLID][$intBT][$Rows -> Number];

		if($_arrBetParam[$intBT][$intAN]["maxOdds"] < $flOdds)$flOdds = $_arrBetParam[$intBT][$intAN]["maxOdds"];
		$flOdds = ConvertOdds($intLType, $intBT, $flOdds);
		foreach($arrBT as $intBT){
			if(!$intOID = InsertBetTypeOdds($intGID, $intLID, $intBT, $Rows -> Number, $flOdds, 0, $_SESSION["Admin_Info"] -> Account)){
				@PMA_mysql_query("ROLLBACK");
				ErrMsgOutputHTML("Error", "", 1);
			}
			if(!UpdateTodayGame($intGID, $intLID, $intBT, $Rows -> Number, $intOID, $flOdds, ", tolJumpStake = 0")){
				@PMA_mysql_query("ROLLBACK");
				ErrMsgOutputHTML("Error", "", 1);
			}
			$arrOID[] = $intOID;
		} //foreach($arrBT as $intBT){
	} //while($Rows = PMA_mysql_fetch_object($RS)){

}else{ //if($isGapUpdate == "1"){

	if($flOdds < 0)$flOdds = 0;
	include_once(__Common_Path."/gambletype/GambleType1_".$intLID.".inc.php");
	$arrGLineAllOdds1 = $_arrGambleTypeAllOdds[$intBT];
	include_once(__Common_Path."/gambletype/GambleType".$_data["GameType"]."_".$intLID.".inc.php");
	$arrGLineAllOdds2 = $_arrGambleTypeAllOdds[$intBT];

	foreach($arrNum as $intNum){
		$intAN = $_arrGLineNumToApointNum[$intLID][$intBT][$intNum];
		if($intAN == null)$intAN = 0;
		$flOddsTmp = $flOdds;
		if($_arrBetParam[$intBT][$intAN]["maxOdds"] < $flOddsTmp)$flOddsTmp = $_arrBetParam[$intBT][$intAN]["maxOdds"];
		$flOddsTmp = ConvertOdds($intLType, $intBT, $flOddsTmp);

		$A_AllOdds = $arrGLineAllOdds1[$intApointNum];
		$preAllOdds = $arrGLineAllOdds2[$intApointNum];
		$flOddsTmp -= ($preAllOdds - $A_AllOdds)/2;
		$flOddsTmp = $flOddsTmp - $flABGapOdds;

		foreach($arrBT as $intSubBT){
			$flGapOdds = 0;
			if(!$intOID = InsertBetTypeOdds($intGID, $intLID, $intSubBT, $intNum, $flOddsTmp+$flGapOdds, 0, $_SESSION["Admin_Info"] -> Account)){
				@PMA_mysql_query("ROLLBACK");
				ErrMsgOutputHTML("Error", "", 1);
			}
			if(!UpdateTodayGame($intGID, $intLID, $intSubBT, $intNum, $intOID, $flOddsTmp+$flGapOdds, ", tolJumpStake = 0")){
				@PMA_mysql_query("ROLLBACK");
				ErrMsgOutputHTML("Error", "", 1);
			}
			$arrOID[] = $intOID;
		} //foreach($arrBT as $intBT){
	} //for($i = 0; $i < sizeof($arrNum); $i++){

} // //if($isGapUpdate == "1"){
@PMA_mysql_query("COMMIT");
print $flOdds."C==";
//產生 TodayGame File
foreach($arrBT as $intSubBT){
	if(!MakeTodayGameFile($intLID, $intSubBT))ErrMsgOutputHTML("Error", "", 1);
}
$strLog = "樂透種類：".$intLID."\n";
$strLog .= "操盤序號：".$intGID."\n";
$strLog .= "下注種類：".$intBT."\n";
$strLog .= "賠率序號：".join(",", $arrOID)."\n";
$strLog .= "賠率：".$flOdds."\n";
$strLog .= "調整賠率差：".$flGapOdds."\n";
$strLog .= "調整方式：".$isGapUpdate."\n";
$strLog .= "號碼：".$strNumber."\n";
AddData_Log($_SESSION["Admin_Info"] -> ID, "批次更改賠率", $strLog);

if($intBT >= 1 && $intBT <= 13){
	/*=================================================================*/
	header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");															  // HTTP/1.0
	?><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<script language="JavaScript">
	<!--
	parent.ReChangeBet();
	//-->
	</script><?php
	exit();
}
?>